package cn.sciento.transfer.domain.entity;

import com.fasterxml.jackson.annotation.JsonInclude;
import cn.sciento.core.exception.CommonException;import io.choerodon.mybatis.annotation.ModifyAudit;
import io.choerodon.mybatis.annotation.VersionAudit;
import io.choerodon.mybatis.domain.AuditDomain;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import java.util.Date;
import java.util.List;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Transient;
import javax.validation.constraints.NotNull;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.hibernate.validator.constraints.NotBlank;
import cn.sciento.boot.platform.lov.annotation.LovValue;
import cn.sciento.core.base.BaseConstants;
import cn.sciento.starter.keyencrypt.core.Encrypt;
import cn.sciento.transfer.domain.repository.ProducerConfigRepository;
import cn.sciento.transfer.infra.constant.Constants;

@ApiModel("数据消息生产配置")
@VersionAudit
@ModifyAudit
@Table(name = "hdtt_producer_config")
@JsonInclude(JsonInclude.Include.NON_NULL)
public class ProducerConfig extends AuditDomain {
  public static final String ENCRYPT_KEY = "cn.sciento.transfer.domain.entity.ProducerConfig";
  
  public static final String FIELD_PRODUCER_CONFIG_ID = "producerConfigId";
  
  public static final String FIELD_SERVICE_CODE = "serviceCode";
  
  public static final String FIELD_TABLE_NAME = "tableName";
  
  public static final String FIELD_TENANT_FLAG = "tenantFlag";
  
  public static final String FIELD_INIT_DS_CODE = "initDsCode";
  
  public static final String FIELD_INIT_DB_CODE = "initDbCode";
  
  public static final String FIELD_INIT_DDL_SQL = "initDdlSql";
  
  public static final String FIELD_INIT_IMPORT_DATA_FLAG = "initImportDataFlag";
  
  public static final String FIELD_ENABLED_FLAG = "enabledFlag";
  
  public static final String FIELD_DESCRIPTION = "description";
  
  public static final String FIELD_TOPIC_GENERATED_TIME = "topicGeneratedTime";
  
  public static final String FIELD_TOPIC_GENERATED_STATUS = "topicGeneratedStatus";
  
  public static final String FIELD_TOPIC_GENERATED_MSG = "topicGeneratedMsg";
  
  public static final String FIELD_INIT_TEMP_TABLE = "initTempTable";
  
  @Encrypt
  @ApiModelProperty("表ID，主键")
  @Id
  @GeneratedValue
  private Long producerConfigId;
  
  @ApiModelProperty("服务编码")
  @NotBlank
  private String serviceCode;
  
  @ApiModelProperty("表名，作为消息topic")
  @NotBlank
  private String tableName;
  
  @ApiModelProperty("是否按租户分发。1是，0否")
  @NotNull
  private Integer tenantFlag;
  
  @ApiModelProperty("初始化-数据源ID")
  @NotNull
  private Long initDsId;
  
  @ApiModelProperty("初始化-数据源编码")
  @NotBlank
  private String initDsCode;
  
  @ApiModelProperty("初始化-DB编码")
  @NotBlank
  private String initDbCode;
  
  @ApiModelProperty("初始化-ddl语句")
  private String initDdlSql;
  
  @ApiModelProperty("初始化-是否导入数据。1是，0否")
  @NotNull
  private Integer initImportDataFlag;
  
  @ApiModelProperty("是否启用。1启用，0未启用")
  @NotNull
  private Integer enabledFlag;
  
  @ApiModelProperty("说明")
  private String description;
  
  @ApiModelProperty("Topic创建时间")
  private Date topicGeneratedTime;
  
  @ApiModelProperty("Topic状态，参考：HDTT.TOPIC_GEN_STATUS")
  @LovValue(lovCode = "HDTT.TOPIC_GEN_STATUS", meaningField = "topicGeneratedStatusMeaning")
  private String topicGeneratedStatus;
  
  @ApiModelProperty("Topic创建消息")
  private String topicGeneratedMsg;
  
  @Transient
  private String serviceName;
  
  @ApiModelProperty("主表ddl")
  @Transient
  private String mainTableDDL;
  
  @Transient
  private String tlTableName;
  
  @Transient
  private String tlTableDDL;
  
  @Transient
  private String initTempTlTable;
  
  @Transient
  private String topicGeneratedStatusMeaning;
  
  @ApiModelProperty("初始化-导入临时表名，用于批量初始化数据到表")
  @Transient
  private String initTempTable;
  
  @Transient
  private String backupTableName;
  
  @Transient
  private String backupTlTableName;
  
  @Transient
  private List<ConsDbConfig> consDbConfigs;
  
  public void validUnique(ProducerConfigRepository producerConfigRepository) {
    ProducerConfig querry = new ProducerConfig();
    querry.setTableName(this.tableName);
    querry.setServiceCode(this.serviceCode);
    List<ProducerConfig> select = producerConfigRepository.select(querry);
    if (CollectionUtils.isNotEmpty(select))
      throw new CommonException("error.produce.config.exist", new Object[] { this.tableName, this.serviceCode }); 
  }
  
  public void init() {
    if (this.tenantFlag == null)
      this.tenantFlag = BaseConstants.Flag.NO; 
    if (this.enabledFlag == null)
      this.enabledFlag = BaseConstants.Flag.YES; 
    if (this.initImportDataFlag == null)
      this.initImportDataFlag = BaseConstants.Flag.YES; 
    this.topicGeneratedStatus = "P";
  }
  
  public void analyzeDDL() {
    if (StringUtils.isEmpty(getInitDdlSql()))
      throw new CommonException("error.data_invalid");
    if (getInitDdlSql().contains("drop table") || getInitDdlSql().contains("delete from"))
      throw new CommonException("error.sql.bad", new Object[] { "sql illegal" }); 
    String[] ddlS = getInitDdlSql().split(";");
    this.mainTableDDL = ddlS[0];
    if (ddlS.length > 1) {
      this.tlTableDDL = ddlS[1];
      setTlTableName(this.tableName + Constants.TableNameSuffix.TLS.suffix());
    } 
  }
  
  public Long getProducerConfigId() {
    return this.producerConfigId;
  }
  
  public void setProducerConfigId(Long producerConfigId) {
    this.producerConfigId = producerConfigId;
  }
  
  public String getServiceCode() {
    return this.serviceCode;
  }
  
  public void setServiceCode(String serviceCode) {
    this.serviceCode = serviceCode;
  }
  
  public String getTableName() {
    return this.tableName;
  }
  
  public void setTableName(String tableName) {
    this.tableName = tableName;
  }
  
  public Integer getTenantFlag() {
    return this.tenantFlag;
  }
  
  public void setTenantFlag(Integer tenantFlag) {
    this.tenantFlag = tenantFlag;
  }
  
  public String getInitDsCode() {
    return this.initDsCode;
  }
  
  public void setInitDsCode(String initDsCode) {
    this.initDsCode = initDsCode;
  }
  
  public String getInitDbCode() {
    return this.initDbCode;
  }
  
  public void setInitDbCode(String initDbCode) {
    this.initDbCode = initDbCode;
  }
  
  public String getInitDdlSql() {
    return this.initDdlSql;
  }
  
  public void setInitDdlSql(String initDdlSql) {
    this.initDdlSql = initDdlSql;
  }
  
  public String getInitTempTable() {
    return this.initTempTable;
  }
  
  public void setInitTempTable(String initTempTable) {
    this.initTempTable = initTempTable;
  }
  
  public Integer getInitImportDataFlag() {
    return this.initImportDataFlag;
  }
  
  public void setInitImportDataFlag(Integer initImportDataFlag) {
    this.initImportDataFlag = initImportDataFlag;
  }
  
  public Integer getEnabledFlag() {
    return this.enabledFlag;
  }
  
  public void setEnabledFlag(Integer enabledFlag) {
    this.enabledFlag = enabledFlag;
  }
  
  public String getDescription() {
    return this.description;
  }
  
  public void setDescription(String description) {
    this.description = description;
  }
  
  public Long getInitDsId() {
    return this.initDsId;
  }
  
  public void setInitDsId(Long initDsId) {
    this.initDsId = initDsId;
  }
  
  public String getServiceName() {
    return this.serviceName;
  }
  
  public void setServiceName(String serviceName) {
    this.serviceName = serviceName;
  }
  
  public String getMainTableDDL() {
    return this.mainTableDDL;
  }
  
  public void setMainTableDDL(String mainTableDDL) {
    this.mainTableDDL = mainTableDDL;
  }
  
  public String getTlTableName() {
    return this.tlTableName;
  }
  
  public void setTlTableName(String tlTableName) {
    this.tlTableName = tlTableName;
  }
  
  public String getTlTableDDL() {
    return this.tlTableDDL;
  }
  
  public void setTlTableDDL(String tlTableDDL) {
    this.tlTableDDL = tlTableDDL;
  }
  
  public Date getTopicGeneratedTime() {
    return this.topicGeneratedTime;
  }
  
  public void setTopicGeneratedTime(Date topicGeneratedTime) {
    this.topicGeneratedTime = topicGeneratedTime;
  }
  
  public String getTopicGeneratedStatus() {
    return this.topicGeneratedStatus;
  }
  
  public void setTopicGeneratedStatus(String topicGeneratedStatus) {
    this.topicGeneratedStatus = topicGeneratedStatus;
  }
  
  public String getTopicGeneratedMsg() {
    return this.topicGeneratedMsg;
  }
  
  public void setTopicGeneratedMsg(String topicGeneratedMsg) {
    this.topicGeneratedMsg = topicGeneratedMsg;
  }
  
  public String getTopicGeneratedStatusMeaning() {
    return this.topicGeneratedStatusMeaning;
  }
  
  public void setTopicGeneratedStatusMeaning(String topicGeneratedStatusMeaning) {
    this.topicGeneratedStatusMeaning = topicGeneratedStatusMeaning;
  }
  
  public String getInitTempTlTable() {
    return this.initTempTlTable;
  }
  
  public void setInitTempTlTable(String initTempTlTable) {
    this.initTempTlTable = initTempTlTable;
  }
  
  public String getBackupTableName() {
    return this.backupTableName;
  }
  
  public ProducerConfig setBackupTableName(String backupTableName) {
    this.backupTableName = backupTableName;
    return this;
  }
  
  public String getBackupTlTableName() {
    return this.backupTlTableName;
  }
  
  public ProducerConfig setBackupTlTableName(String backupTlTableName) {
    this.backupTlTableName = backupTlTableName;
    return this;
  }
  
  public List<ConsDbConfig> getConsDbConfigs() {
    return this.consDbConfigs;
  }
  
  public ProducerConfig setConsDbConfigs(List<ConsDbConfig> consDbConfigs) {
    this.consDbConfigs = consDbConfigs;
    return this;
  }
  
  public String toString() {
    return (new ToStringBuilder(this))
      .append("producerConfigId", this.producerConfigId)
      .append("serviceCode", this.serviceCode)
      .append("tableName", this.tableName)
      .append("tenantFlag", this.tenantFlag)
      .append("initDsId", this.initDsId)
      .append("initDsCode", this.initDsCode)
      .append("initDbCode", this.initDbCode)
      .append("initDdlSql", this.initDdlSql)
      .append("initTempTable", this.initTempTable)
      .append("initImportDataFlag", this.initImportDataFlag)
      .append("enabledFlag", this.enabledFlag)
      .append("description", this.description)
      .append("topicGeneratedTime", this.topicGeneratedTime)
      .append("topicGeneratedStatus", this.topicGeneratedStatus)
      .append("topicGeneratedMsg", this.topicGeneratedMsg)
      .append("serviceName", this.serviceName)
      .append("mainTableDDL", this.mainTableDDL)
      .append("tlTableName", this.tlTableName)
      .append("tlTableDDL", this.tlTableDDL)
      .append("initTempTlTable", this.initTempTlTable)
      .append("topicGeneratedStatusMeaning", this.topicGeneratedStatusMeaning)
      .append("backupTableName", this.backupTableName)
      .append("backupTlTableName", this.backupTlTableName)
      .toString();
  }
}
